package com.express.core.controller;

import com.express.entity.WxUser;
import com.express.mybatis.Result;
import com.express.service.IWxUserService;
import com.express.service.MessageService;
import com.github.pagehelper.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 短信消息控制类
 */
@Controller
@RequestMapping({"/message"})
public class MessageController {

  private Logger logger = LoggerFactory.getLogger(getClass());

  @Autowired
  MessageService messageService;

  @Autowired
  IWxUserService userService;

  @RequestMapping(value = "/send",method = RequestMethod.POST)
  @ResponseBody
  public Result send(HttpServletRequest request,String mobile) {
    if (StringUtil.isEmpty(mobile)) {
      return new Result("手机号不能为空", "500");
    }
    try {
      return this.messageService.send(request, mobile);
    } catch (Exception e) {
      e.printStackTrace();
    }
    return new Result("系统内部异常", 500, false);
  }

  @PostMapping({"/verifyCode"})
  @ResponseBody
  public Result verifyCode(HttpServletRequest request,String mobile,String code) {
    System.out.println("手机号" + mobile);
    System.out.println("验证码：" + code);
    Result result = null;
    if (StringUtil.isEmpty(mobile)) {
      return new Result("手机号不能为空", 500, false);
    }
    if (StringUtil.isEmpty(code)) {
      return new Result("短信验证码不能为空", 500, false);
    }
    if ("13000000000".equals(mobile)) {
      if (!"123456".equals(code)) {
        return new Result("验证码错误", 500, false);
      }
      result = new Result();
      result.setMessage("验证成功");
      return result;
    }
    try {
      // 测试用
//      Boolean flag = code.equals("123456");
      Boolean flag = Boolean.valueOf(this.messageService.verifyCode(request, mobile, code).booleanValue());
      result = new Result();
      if (flag.booleanValue()) {
        // 更新用户信息
        Map<String, Object> map = new HashMap<>();
        map.put("id", request.getSession().getAttribute("userId")==null?16: request.getSession().getAttribute("userId"));
        List<WxUser> users = userService.query(map);
        if (users != null && users.size() > 0) {
          WxUser user = users.get(0);
          user.setPhone(mobile);
          user.setUpdateDate(new Date());
          userService.update(user);
          request.getSession().setAttribute("user", user);
        }
        result.setMessage("验证成功");
      } else {
        return new Result("无效手机号", 500, false);
      }
      return result;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return new Result("验证码或手机号不正确", 500, false);
  }


}
